package com.wtao.testcamera;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.graphics.SurfaceTexture;
import android.os.Bundle;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;

import com.senseauto.subgroup.autocameralib.CameraFactory;
import com.senseauto.subgroup.autocameralib.callback.CameraImageCallback;
import com.senseauto.subgroup.autocameralib.cameraV2.CameraV2Proxy;
import com.senseauto.subgroup.autocameralib.model.CameraOptions;

public class CameraLibActivity extends AppCompatActivity {
    String TAG ="CameraLibActivity";
    TextureView textureView;
    CameraFactory cameraV2Proxy;
    int cameraId = 45;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera);
        textureView = findViewById(R.id.texture);

        textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(@NonNull SurfaceTexture surface, int width, int height) {
                initOpenCamera();
            }

            @Override
            public void onSurfaceTextureSizeChanged(@NonNull SurfaceTexture surface, int width, int height) {

            }

            @Override
            public boolean onSurfaceTextureDestroyed(@NonNull SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(@NonNull SurfaceTexture surface) {

            }
        });
        findViewById(R.id.switchCamera).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                if (cameraV2Proxy != null) {
                    cameraV2Proxy.releaseCamera();
                    cameraV2Proxy = null;
                }
                if (cameraId == 45) {
                    cameraId = 49;
                } else if (cameraId == 49) {
                    cameraId = 45;
                }
                Log.e(TAG, "切换摄像头,最新cameraId="+cameraId);
                CameraOptions cameraOptions = new CameraOptions();
                cameraOptions.setCameraId(cameraId);
                cameraOptions.setImgWidth(1600);
                cameraOptions.setImgHeight(1300);
                if (cameraV2Proxy == null) {
                    cameraV2Proxy = CameraFactory.getInstance().createCameraV2Proxy(CameraLibActivity.this, cameraOptions, new CameraImageCallback() {
                        @Override
                        public void onReadImageData(@Nullable byte[] bytes, int i, int i1) {

                        }

                        @Override
                        public void onException(@NonNull Exception e) {

                        }
                    }, new Surface(textureView.getSurfaceTexture()));
                }
                textureView.setScaleX(-1f);
                Log.e(TAG, "打开camera");
                cameraV2Proxy.openCamera();
            }
        });

    }
    public void initOpenCamera(){
        CameraOptions cameraOptions = new CameraOptions();
        cameraOptions.setImgWidth(1600);
        cameraOptions.setImgHeight(1300);
        //德赛车机A02的cameraId变成了45和49，不再是0和1了
        Log.e("initCamera", "cameraId="+cameraId);
        cameraOptions.setCameraId(cameraId);
        cameraV2Proxy = CameraFactory.getInstance().createCameraV2Proxy(this, cameraOptions, new CameraImageCallback() {

            @Override
            public void onReadImageData(@Nullable byte[] bytes, int width, int height) {
                //Log.e(TAG, "width="+width+" height="+height);
            }

            @Override
            public void onException(@NonNull Exception e) {

            }
        }, new Surface(textureView.getSurfaceTexture()));
        Log.e(TAG, "initCamera() mCameraFactory.openCamera()");
        textureView.setScaleX(-1f);
        cameraV2Proxy.openCamera();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        cameraV2Proxy.releaseCamera();
    }
}